import {folderService} from '../../service/folderService'

export default {
  state: {
    folderTree: [],
    childrenFolder: [],
    currentPath: { // 当前路径
      paths: ['工作流程'],
      folderId: 1
    },
    isAddFolder: false,
    folderPath: '',
    // 存放已访问的路径
    backPathStack: [],
    // 存放后退之后的路径
    forwardPathStack: []
  },
  mutations: {
    FIND_ALL_FOLDERS (state, userId) {
      folderService.findAllFolders({
        userId: userId
      }).then(res => {
        state.folderTree = res.data
      })
    },
    SET_PATHS (state, row) {
      if (state.currentPath.paths.length > 0) {
        this.commit('SET_BACKPATHSTACK') // 将当前路径存入栈中
      }
      state.forwardPathStack = [] // 清空前进栈
      state.currentPath.paths = row.nextPath
      state.currentPath.folderId = row.nextFolderId
      this.commit('SET_FOLDER_PATH')
    },

    SET_BACKPATHSTACK (state) { // 将当前页面存入栈中
      let pathObject = {}
      pathObject.folderId = state.currentPath.folderId
      pathObject.paths = state.currentPath.paths
      state.backPathStack.push(pathObject)
    },
    PATH_BACKOFF (state) { // 后退
      let path = state.backPathStack.pop()
      state.forwardPathStack.push(state.currentPath)
      state.currentPath = path
      this.commit('SET_FOLDER_PATH')
    },
    PATH_FORWARD (state) { // 前进
      let path = state.forwardPathStack.pop()
      state.backPathStack.push(state.currentPath)
      state.currentPath = path
      this.commit('SET_FOLDER_PATH')
    },
    SET_FOLDER_PATH (state) {
      let path = '/'
      let value = state.currentPath.paths
      value.forEach(v => {
        path += v + '/'
      })
      state.folderPath = path.substring(0, path.length - 1)
    }
  }
}
